package cn.cupster.flowdemo.fragment

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import cn.cupster.flowdemo.ListPageActivity
import cn.cupster.flowdemo.LoginActivity
import cn.cupster.flowdemo.R
import cn.cupster.flowdemo.bean.Vp2PageBean
import cn.cupster.flowdemo.fragment.Vp2PageFragment.Companion.KEY_ARGUMENT_PAGE_BEAN

class Vp2PageFirstFragment : Fragment() {

    companion object {

        fun create(pageBean: Vp2PageBean?): Fragment {
            return Vp2PageFirstFragment().apply {
                arguments = Bundle().apply {
                    putSerializable(KEY_ARGUMENT_PAGE_BEAN, pageBean)
                }
            }
        }
    }

    private lateinit var mBtnLogin: TextView
    private lateinit var mBtnList: TextView

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val fragLayout = inflater.inflate(R.layout.fragment_vp2_first_page, container, false)
        mBtnLogin = fragLayout.findViewById(R.id.btnLogin)
        mBtnList = fragLayout.findViewById(R.id.btnList)
        return fragLayout
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val pageBean: Vp2PageBean? =
            arguments?.getSerializable(KEY_ARGUMENT_PAGE_BEAN) as? Vp2PageBean
        updateData(pageBean)
        mBtnLogin.setOnClickListener {
            openActivity(LoginActivity::class.java)
        }
        mBtnList.setOnClickListener {
            openActivity(ListPageActivity::class.java)
        }
    }

    fun updateData(pageBean: Vp2PageBean?) {
        pageBean?.let {

        }
    }

    fun <T : AppCompatActivity> openActivity(clazz: Class<T>) {
        val target = Intent(context, clazz)
        startActivity(target)
    }
}